/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.modeldocument;

import java.io.Serializable;

public class AttributePrototype implements Serializable, Cloneable {
	
	/**
	 * The type of filterValue on an attribute. EQUALS will do exact mathces on an attribute, whereas
	 * CONTAINS will check whether the filterValue value is contained within the attribute (applicable
	 * to string attributes)
	 * @author bjnortier
	 *
	 */
	public enum FilterType {
		EQUALS,
		CONTAINS
	}
	
	/**
	 * @param name The attribute name to filterValue
	 * @param filterType The type of filterValue, i.e. EQUALS or CONTAINS
	 * @param filterValue The filterValue value
	 */
	public AttributePrototype(String name,FilterType filterType,String filterValue) {
		m_name = name;
		m_filterValue = filterValue;
        m_filterType = filterType;
	}
	
	
	/**
	 * @return the m_filterType
	 */
	public FilterType getFilterType() {
        return m_filterType;
	}

	/**
	 * @param type the m_filterType to set
	 */
	public void setFilterType(FilterType type) {
        m_filterType = type;
	}

	/**
	 * @return The filter value
	 */
	public String getFilterValue() {
		return m_filterValue;
	}

    /**
     * @return The dgbQL representation of the filter value
     */
    public String getdgbQLFilterValue() {
        if ( getFilterType() == FilterType.CONTAINS ) {
            return "%" + m_filterValue + "%";
        } else {
            return m_filterValue;
        }
    }
    
	/**
	 * @param value the m_filterValue to set
	 */
	public void setFilterValue(String value) {
		m_filterValue = value;
	}
   

	/**
	 * @return the m_name
	 */
	public String getName() {
		return m_name;
	}

	/**
	 * @param m_name the m_name to set
	 */
	public void setname(String m_name) {
		this.m_name = m_name;
	}

	/**
	 * Return a string representation of the attribute prototype
	 */
	public String toString() {
		String str = m_name;
		if ( getFilterType() == FilterType.EQUALS ) {
			str += " = ";
		}
		else {
			str += " contains ";
		}
		str += "\"" + m_filterValue + "\"";
		return str;
	}
    
	// ------------------- Cloning -------------
    
    public Object clone() { 
        AttributePrototype o = null;
        try {
            o = (AttributePrototype)super.clone();
        } catch(CloneNotSupportedException e) {
            System.err.println("Definition can't clone");
        }
        return o;
    }
    
	// ------------------- Serialization -------------
    
    /**
     * First serilization version
     */
    private static final long serialVersionUID = 1L;
    

    
    // ------------------- Fields ---------------------

	/// The filter type
	private FilterType m_filterType;
	/// The name of the attribute to filter
	private String m_name;
	/// The value to filter with
	private String m_filterValue;

}
